package com.ssy.lingxi.report.dao.report;

import com.ssy.lingxi.report.model.bo.StatisticsOrderBO;
import com.ssy.lingxi.report.model.bo.StatisticsOrderNewlyAddedBO;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单新增记录DAO
 * @author 伍将
 * @date 2021/2/20
 * @version 2.0.0
 */
@Service
public class OrderAddRecordDAO {

    @Resource
    @Qualifier("reportJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    /**
     * 统计每日新增订单
     * @author 伍将
     * @date 2021/2/20
     * @param startTime: 开始时间
     * @param endTime: 结束时间
     * @return 统计结果
     **/
    public List<StatisticsOrderNewlyAddedBO> getNewlyAddedOrderByDay(LocalDateTime startTime, LocalDateTime endTime) {
        List<StatisticsOrderBO> statisticsGroupByDay = getStatisticsGroupByDay(startTime, endTime, null, null);
        if(statisticsGroupByDay==null||statisticsGroupByDay.size()==0){
            return null;
        }
        return statisticsGroupByDay.stream().map(o ->{
            StatisticsOrderNewlyAddedBO bo=new StatisticsOrderNewlyAddedBO();
            bo.setAmount(o.getAmount());
            bo.setDateTime(o.getDateTime());
            return bo;
        }).collect(Collectors.toList());
    }

    /**
     * 统计日新增订单
     * @author 伍将
     * @date 2021/2/20
     * @param startTime: 开始时间
     * @param endTime: 结束时间
     * @return 统计结果
     **/
    public List<StatisticsOrderBO> getStatisticsOrderByDay(LocalDateTime startTime, LocalDateTime endTime) {
        return getStatisticsGroupByDay(startTime,endTime,null,null);
    }

    /**
     * 统计会员的每日订单
     * @author 伍将
     * @date 2021/2/20
     * @param startTime: 开始时间
     * @param endTime: 结束时间
     * @return 统计结果
     **/
    public List<StatisticsOrderBO> getStatisticsDayOrderByMember(LocalDateTime startTime, LocalDateTime endTime,Long memberId,Long roleId) {
        return getStatisticsGroupByDay(startTime,endTime,memberId,roleId);
    }

    private List<StatisticsOrderBO> getStatisticsGroupByDay(LocalDateTime startTime, LocalDateTime endTime,Long memberId,Long roleId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select to_char(date_time,'YYYY-MM-dd') date_time,sum(amount) amount,count(id) count ")
                .append(" from rep_order_add_record")
                .append(" where date_time between '" + startTime + "' and '" + endTime + "'");
        if (memberId!=null){
            sql.append(" and member_id="+memberId+" and role_id="+roleId);
        }else {
            sql.append(" and member_type=1");
        }
        sql.append(" group by to_char(date_time,'YYYY-MM-dd');");

        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(StatisticsOrderBO.class));
    }

    /**
     * 统计月新增订单
     * @author 伍将
     * @date 2021/2/20
     * @param startTime: 开始时间
     * @param endTime: 结束时间
     * @return 统计结果
     **/
    public List<StatisticsOrderBO> getStatisticsOrderByMonth(LocalDateTime startTime, LocalDateTime endTime) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select to_char(date_time,'YYYY-MM-01') date_time,sum(amount) amount,count(id) count ")
                .append(" from rep_order_add_record")
                .append(" where date_time between '" + startTime + "' and '" + endTime + "'")
                .append(" group by to_char(date_time,'YYYY-MM-01');");

        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(StatisticsOrderBO.class));
    }
}
